<?php

namespace System\Configure\Adapter;

use System\Configure\AdapterAbstract;
use System\Configure\Exception\ParserXmlException;

/**
 * XML配置文件解析类
 *
 * @package Dream
 * @subpackage Xml
 * @namespace System\Configure\Adapter
 * @author lewis (lewisld@yeah.net)
 */
class Xml extends AdapterAbstract
{

    /**
     * XML配置文件解析后的数组
     *
     * @static
     * @access protected
     * @var array
     */
    protected static $data = [];

    /**
     * 解析XML配置文件
     *
     * @static
     * @access public
     * @param string $path XML文件路径
     * @return array 返回配置数组
     * @throws ParserXmlException 解析XML配置文件到数组异常
	 */
    public static function parser($path)
    {
        libxml_use_internal_errors(true);
        $data = simplexml_load_file($path, null, LIBXML_NOERROR);
        if ($data === false) {
            $errors = array_pop(libxml_get_errors());
            $error = array(
                'message' => 'system.error.conf.parser.xml',
                'type' => $errors->level,
                'code' => $errors->code,
                'file' => $errors->file,
                'line' => $errors->line,
                'xmlfile' => $path,
            );
            throw new ParserXmlException($error);
        }
        self::$data = json_decode(json_encode($data), true);
        unset($data, $errors, $error);
        return self::$data;
    }
}
